package com.interagriculture.utils;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.interagriculture.common.Constants;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * @author 11 2024/11/9
 */
@Component
public class CacheContainerUtil {
     private static Cache<String, String> cacheContainerCheckCode;
     private static Cache<String,String> cacheContainerSession;

     public static void putCheckCode(String key,String value){
          cacheContainerCheckCode.put(key,value);
     }
     public static void removeCheckCode(String key){
          cacheContainerCheckCode.invalidate(key);
     }
     public static String getCheckCode(String key){
          return cacheContainerCheckCode.getIfPresent(key);
     }

     public static void putSession(String key,String value){
          cacheContainerSession.put(key,value);
     }
     public static void removeSession(String key){
          cacheContainerSession.invalidate(key);
     }
     public static String getSession(String key){
          return cacheContainerSession.getIfPresent(key);
     }

     public static boolean isEmpty(String key){
          if(cacheContainerSession.getIfPresent(key)==null){
               return true;
          }
          return false;
     }
     @PostConstruct
     public static void newCacheContainer(){
          if(cacheContainerSession == null){
               cacheContainerSession =
                       CacheBuilder.newBuilder().expireAfterWrite(Constants.一天*2, TimeUnit.MINUTES).build();
          }
          if(cacheContainerCheckCode == null){
               cacheContainerCheckCode =
                       CacheBuilder.newBuilder().expireAfterWrite(Constants.一分钟*5, TimeUnit.MINUTES).build();
          }
     }
}
